<?php
require_once(APPPATH."models/base.php");

class article_model extends base_model
{	
	function title($len)
	{
		if(mb_strlen($this->title,'utf8') <= $len)
		{
			return $this->title ;  
		}
		else
		{ 
			return mb_substr($this->title, 0,$len,"utf8")."..." ;
		}
	}
	
	function get_wrap_content()
	{
		// $this->load->plugin("comment");
		// $this->content = $this->comment_plugin->execute($this->content); 
		return $this->content;
	}
	
	function click_count($key = 'get')
	{
		if($key == "get")
		{
			return $this->click_count;
		}
		else
		{
			/*
			$t_article = _T("article");
			$sql = "update $t_article set click_count=$key where id=".$this->id;
			$this->db->query($sql);*/
			$this->db->update('article',array('click_count'=>$key),array('id'=>$this->id));
			$this->click_count = $key; 
			return $this->click_count ; 
		}
	}
	
	function click()
	{
		return $this->click_count($this->click_count()+1); 
	}
	
	function get_class()
	{
		$article_class = $this->article_class_manager->get($this->class_id);
		return $article_class;
		
	}
	
	function get_creator()
	{
		//return $this->article_class_manager->get($this->class_id);
	}
	
	function get_seo()
	{
		return $this->db->get_where('seo',array('type'=>'article','key'=>$this->id));
	}
	
	function get_text()
	{
		return strip_tags($this->content);
	}
	
	function get_profile($length)
	{
		$text = str_replace( '&nbsp;', '', $this->text);  
		$text = str_replace( "\r\n", '', $text);    
		$text = str_replace( '\r', '', $text);      
		$text = str_replace( '\n', '', $text);  
		if(mb_strlen($text) > $length)
		{
			return mb_substr($text, 0,$length,'utf8')."..." ; 
		}  
		else
		{
			return $text ; 
		}     
	}
	 
	function fetch_images($model = Fetch_Image_Model::OnlySrc)
	{ 
		if($model == Fetch_Image_Model::OnlySrc)
		{
			$pattern = "/<img .*?src=\"(.+?)\".*?>/";
			preg_match_all($pattern, $this->content, $m);
			$images = array();
			if(isset($m[1]))
			{
				foreach ($m[1] as $image)
				{
					$images[] = $image;
				}
			}
			return $images;
		}
		else
		{
			$pattern = "/<img .*?src=(.+?).*?>/";
			preg_match_all($pattern, $this->content, $m);
			// print_r($m);
			$ob_images = Array();
			if(isset($m[0]))
			{
				foreach($m[0] as $img_html)
				{
					$ob_image = new stdClass();
					$p = "/src=\"(.+?)\"/";
					preg_match($p, $img_html,$img);
					if(isset($img[1]))
					{
						$ob_image->src = $img[1];
						$p = "/title=\"(.+?)\"/";
						preg_match($p, $img_html,$title);
						$ob_image->title = isset($title[1])?$title[1]:'';
						$p = "/alt=\"(.+?)\"/";
						preg_match($p, $img_html,$alt);
						$ob_image->alt = isset($alt[1])?$alt[1]:'';
					}
					$ob_images[] = $ob_image;
				}
			}
			return $ob_images ;  
		} 
	}
   	
	function fetch_image($index,$model = Fetch_Image_Model::OnlySrc)
	{
		$images = $this->fetch_images();
		if(isset($images[$index]))
		{
			return $images[$index];
		}
		return null ; 
	}
}

class article_manager extends base_manager
{
	private $maps = array('creator'=>'user','class'=>'article_class');
	
	public function get_list($where = Array())
	{
		$class_alias = array_remove($where, 'class.alias');
		$class_id = array_remove($where, 'class.id');
		$descendant = array_remove($where, 'descendant');
		//echo $class_alias;exit;
		$article_class = null;
		if($class_id != null)
		{
			$article_class = $this->article_class_manager->get($class_id);
		}
		if($class_alias != null)
		{
			$article_class = $this->article_class_manager->get(array('alias'=>$class_alias));
		}
		if($article_class!=null)  // 类ID不为空的情况 
		{
			$descendant == null && $descendant = 'false';
			
			if($descendant == "false")
			{
				$where['class.id'] = $article_class->id;
			}
			else
			{
				$ids = Array($article_class->id);
				// print_r($ids);
				$descendant = $article_class->descendant;
				if(!function_exists('article_cube'))
				{
					function article_cube($v)
					{
						return $v->id;
					}
				}
				$ids = array_merge($ids,array_map('article_cube',$descendant));
				// print_r($ids);exit;
				$ids = trim(implode(",", $ids),','); 
				$where['class.id'] = "in(".$ids.")";
			}
		}
		$query_result = new stdClass();
		$page = array_remove($where,'page');$page == null && $page = 1 ;
		$page_size = array_remove($where,'page_size');
		
		$order_by = array_remove($where,'order_by');
		
		$t_article = _T("article");$t_article_class = _T("article_class");
		
		
		$where_string = "where 1=1 ";
		
		$where_string .= $this->build_where_string($where);
		
		$sql_pattern =
		@"select %s from $t_article article
		left join $t_article_class class on article.class_id=class.id %s";
		
		$count_sql = sprintf($sql_pattern,"count(*) as row_number",$where_string);
		$query_result->record_count = $this->db->query($count_sql)->row()->row_number;
		
		if($order_by != null)
		{
			$sql_pattern .= " order by $order_by";
		}
		else
		{
			$sql_pattern .= " order by id desc";
		}
		// var_dump($page_size);exit;
		if($page_size != null)
		{
			$begin_index = ($page-1)*$page_size;
			$query_result->page_count = intval($query_result->record_count/$page_size)-1;
			$sql_pattern .= " limit $begin_index,$page_size";
		}
		
		$result_sql = sprintf(
			$sql_pattern,
			@"article.*",
			$where_string
		);
		// echo $result_sql;exit; 
		$this->query_result = $query_result;
		return $this->convert_list(
				$this->db->query($result_sql)->result()
		); 
	}
	
	public function moveto($class_id,$article_id) 
	{ 
		$this->update(array("class_id"=>$class_id), array("id"=>$article_id)) ;
	}  
}

?>